package com.wondertek.mam.tag.rep.impl;

import com.wondertek.mam.tag.domain.TagDB;
import com.wondertek.mam.tag.domain.TagWord;
import com.wondertek.mam.tag.rep.TagRep;
import com.wondertek.mam.tag.repository.TagRepository;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * Jpa实现
 * Created by wangdongxu on 2017-09-06 上午 11:08:43.
 */
public class  TagSqlRep implements TagRep {

    private TagRepository tagRepository;

    public TagSqlRep() {
    }

    public TagSqlRep(TagRepository tagRepository){
        this.tagRepository = tagRepository;
    }

    @Override
    public Collection<TagWord> getTagWordWithScore(List<String> wordsList) {
        List<TagDB> allByTagIn = tagRepository.findAllByTagIn(wordsList);
        if (allByTagIn.size() ==0){
            return new ArrayList<>();
        }
        Map<String, TagWord> resultMap = new HashMap<>();
        allByTagIn.stream().map(tagDB -> new TagWord(tagDB.getTag(),tagDB.getScore())).forEach(tagWord -> {
            resultMap.putIfAbsent(tagWord.getWord(),tagWord);
            if(resultMap.get(tagWord.getWord()).getScore()<tagWord.getScore())
                resultMap.put(tagWord.getWord(),tagWord);
        });
        return resultMap.entrySet().parallelStream().map(Map.Entry::getValue).collect(Collectors.toList());
    }
}
